--
-- Author: shunguo.chen
-- Date: 2016-06-17 11:42:18
--
-- ViewBase
--

local fileName = string.getModeName(...)
local ViewBase = class(fileName, function(viewType)
	local node
	if viewType == app.mvc.VIEWTYPE_NORMAL then
    	node = display.newLayer()
    -- VIEWTYPE_BLUR 不继承自display.newBlurDialog，而是通过 VIEWTYPE_BLUR 类型在systemUI里面去管理 blurDialog
	-- elseif viewType == app.mvc.VIEWTYPE_BLUR then
	-- 	node = display.newBlurDialog()
	elseif viewType == app.mvc.VIEWTYPE_MODAL_BG then
		node = display.newModalDialog()
	else
		node = display.newModalDialogWithoutColor()
    end
    node:setNodeEventEnabled(true)
    return node
end)

function ViewBase.createViewClass(...)
    local className = string.getModeName(...)
    local ClassRef = class(className, ViewBase)
    return ClassRef
end

ViewBase.VIEW_TYPE = nil

ViewBase.VIEW_ROOT_RES = ""

ViewBase._root = nil
ViewBase._uiBinding = nil
ViewBase._ui = nil
ViewBase._data = nil

ViewBase._onHideView = nil
ViewBase._onCloseView = nil
ViewBase._onClearView = nil
ViewBase._onOperate = nil

local executeFunc = _G["executeFunc"]

local OPEN_ANI_NAME = "open"
local CLOSE_ANI_NAME = "close"

function ViewBase:ctor(viewType, data)
	self.VIEW_TYPE = viewType or app.mvc.VIEWTYPE_MODAL
	--dump(data, "Viewbase.data")
	-- print("Viewbase.viewType", self.VIEW_TYPE)
	-- print("ViewBase.crtor()", self.__cname)

	-- debug.addMonitor(self, self.__cname)

	self._data = data

    self:initRoot()
    self:init()
end

function ViewBase:getData()
	return self._data
end

function ViewBase:initRoot()
	--init root
	self._root, self._uiBinding = cc.CSLoader:createNode(self.VIEW_ROOT_RES, true, true, true)

	if self.VIEW_TYPE == app.mvc.VIEWTYPE_BLUR or self.VIEW_TYPE == app.mvc.VIEWTYPE_MODAL then
		self._root:setContentSize(display.ui_width, display.ui_height)
		self._root:setAnchorPoint(0.5, 0.5)
		self._root:setPosition(display.width/2, display.height/2)
		display.setAutoScale(self._root)
		ccui.Helper:doLayout(self._root)
	else
		display.doLayout(self._root)
	end

	self._root:setName(self:getViewName())
	self._ui = display.uiDelegate(self._root)
	self:addChild(self._root)

	-- timeline init
	local timeline = self._root:getTimeline()
	if timeline then
		timeline:pause()
		self._hasOpenAct = timeline:IsAnimationInfoExists(OPEN_ANI_NAME)
		self._hasCloseAct = timeline:IsAnimationInfoExists(CLOSE_ANI_NAME)
		if self._hasOpenAct then
			local info = timeline:getAnimationInfo(OPEN_ANI_NAME)
			self._openDuration = info.endIndex - info.startIndex
		end
		if self._hasCloseAct then
			local info = timeline:getAnimationInfo(CLOSE_ANI_NAME)
			self._closeDuration = info.endIndex - info.startIndex
		end

		-- 暂时只需要监听close动画的完成(有点问题)
		-- if self._hasCloseAct then
		-- 	timeline:setLastFrameCallFunc(function()
		-- 		print("ViewBase.setLastFrameCallFunc", self._timelineAniName)
		-- 		if self._timelineAniName == CLOSE_ANI_NAME then
		-- 			self:setVisible(false)
		-- 			executeFunc(self._hideCB)
		-- 			self._hideCB = nil
		-- 		end
		-- 	end)
		-- end

		self._timeline = timeline
	end
end

function ViewBase:init()

end

function ViewBase:onHideView(cb)
	self._onHideView = cb
end

function ViewBase:onCloseView(cb)
	self._onCloseView = cb
end

function ViewBase:onClearView(cb)
	self._onClearView = cb
end

function ViewBase:onOperate(cb)
	self._onOperate = cb
end

function ViewBase:openAction(isLoop)
	-- print("ViewBase:openAction()")
    isLoop = isLoop or false
    if self._hasOpenAct then
        self:playAction(OPEN_ANI_NAME, isLoop)
    else
    	if self._timeline then
        	self._timeline:gotoFrameAndPlay(0, isLoop)
    	end
    end
end

function ViewBase:closeAction(cb)
    if self._hasCloseAct then
        self:playAction(CLOSE_ANI_NAME, false)
        self:performWithDelay(function()
                self:setVisible(false)
                executeFunc(cb)
            end, app:getAnimationInterval() * self._closeDuration)

        -- self._hideCB = cb
    else
        self:setVisible(false)
        executeFunc(cb)
    end
end

function ViewBase:playAction(name, isLoop)
	if self._timeline then
		self._timelineAniName = name
		if name then
    		self._timeline:play(name, isLoop)
    	else
    		self._timeline:gotoFrameAndPlay(0, isLoop)
    	end
	end
end

function ViewBase:pauseAction()
	if self._timeline then
    	self._timeline:gotoFrameAndPause(0)
	end
end

function ViewBase:show()
    self:setVisible(true)
	self:openAction()
end

function ViewBase:hide(cb)
    self:closeAction(cb)
end

function ViewBase:closeView(ignorAction)
	if ignorAction then
		executeFunc(self._onCloseView, self)
	else
		self:closeAction(function()
			executeFunc(self._onCloseView, self)
		end)
	end
end

-- function ViewBase:close()
--     self:removeFromParent()
-- end

function ViewBase:onEnter()

end

function ViewBase:onExit()
	-- print("ViewBase.onExit()", self.__cname)
	self:___deleteViewEvent()
	executeFunc(self._onClearView)
	self:remove()
end

function ViewBase:remove()

end

function ViewBase:getViewType()
	return self.VIEW_TYPE
end
function ViewBase:getViewName()
	return self.__cname
end

-- 添加一个 注册view 事件的函数 
function ViewBase:registerViewEvent(config)
    self:___deleteViewEvent()
    local viewEventHandlers = {}
    for k ,v in pairs(config) do
        local h = EM:on(v.name, v.callback)
        table.insert(viewEventHandlers, h)
    end
    self.___viewEventHandlers = viewEventHandlers
    self:enableNodeEvents()
end

function ViewBase:___deleteViewEvent()
	if self.___viewEventHandlers then
        for k ,v in pairs(self.___viewEventHandlers) do
            EM:remove(v)
        end
        self.___viewEventHandlers = nil
    end
end

return ViewBase
